import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_elder/page/elder_search_page.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/viewmodel/model/combo_data_model.dart';
import 'package:oktoast/oktoast.dart';

class AddMedicalViewModel extends BaseViewModel {
  AddMedicalViewModel(BuildContext context) : super(context);

  var _dataModel = ComboDataModel();

  init() {
    addModels([_dataModel]);
  }

  ElderInfo? older;
  var medicalCauseType = "";
  var medicalCauseTypeName = "";
  var medicalReasons = "";
  var medicalTreatmentType = "";
  var medicalTreatmentTypeName = "";
  var medicalTimeType = "";
  var medicalTimeTypeName = "";
  var medicalPlan = "";
  var medicalPlanName = "";
  var leaveTime = "";
  var backTime = "";

  var _format = [yyyy, "-", mm, "-", dd," ",HH,":",nn,":",ss];

  void selectLeaveTime() {
    DatePicker.showDateTimePicker(context, onConfirm: (date) {
      if(backTime.isNotEmpty){
        if(date?.isAfter(DateTime.parse(backTime))??false){
          showToast("离开时间不能晚于返回时间");
          return;
        }
      }
      leaveTime = date?.toDateStr(_format)??"";
      notifyListeners();
    });
  }

  void selectBackTime() {
    DatePicker.showDateTimePicker(context, onConfirm: (date) {
      if(leaveTime.isNotEmpty){
        if(date?.isBefore(DateTime.parse(leaveTime))??false){
          showToast("返回时间不能早于离开时间");
          return;
        }
      }
      backTime = date?.toDateStr(_format)??"";
      notifyListeners();
    });
  }


  Future<void> selectOlder() async {
    older = await Go().push(ElderSearchPage(
      needBack: true,
      isSelected: false,
    ));
    notifyListeners();
  }

  Future<void> selectBaseData(String dataId) async {
    showLoadingDialog();
    try {
      var data = await _dataModel.getComboData(dataId);
      hideDialog();
      DataPickerView(context, data, onSubmitListener: (p1, p2, p3) {
        var item = data[p1];
        switch (dataId) {
          case ComboDataModel.diseaseReason:
            medicalCauseType = item.id.toString();
            medicalCauseTypeName = item.name;
            break;
          case ComboDataModel.medicalWay:
            medicalTreatmentType = item.id.toString();
            medicalTreatmentTypeName = item.name;
            break;
          case ComboDataModel.medicalTimeType:
            medicalTimeType = item.id.toString();
            medicalTimeTypeName = item.name;
            break;
          case ComboDataModel.medicalPlan:
            medicalPlan = item.id.toString();
            medicalPlanName = item.name;
            notifyListeners();
            break;
          default:
            break;
        }
        notifyListeners();
      }).show();
    } catch (e) {
      hideDialog();
      print(e);
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> submitMedical() async {
    if (older == null) {
      showToast("请选择长者");
      return;
    }
    if (medicalCauseType.isEmpty) {
      showToast('请选择病因类型');
      return;
    }
    if (medicalReasons.isEmpty) {
      showToast("请输入就医原因");
      return;
    }
    if (medicalTreatmentType.isEmpty) {
      showToast("请选择就医形式");
      return;
    }
    if (medicalTimeType.isEmpty) {
      showToast("请选就医时间");
      return;
    }

    if (medicalPlan.isEmpty) {
      showToast("请选择就医计划");
      return;
    }

    if (leaveTime.isEmpty) {
      showToast("请选择离开机构时间");
      return;
    }

    if (backTime.isEmpty) {
      showToast("请选择返回机构时间");
      return;
    }

    showLoadingDialog(msg: "提交中...");
    try {
      var map = await PostRequest.addMedical(
          older?.id.toString()??"",
          medicalCauseType,
          medicalReasons,
          medicalTreatmentType,
          medicalTimeType,
          medicalPlan,
          leaveTime,
          backTime);
      hideDialog();
      // var bean = ResponseData.fromJson(map);
      if("0"==map["code"].toString()){
        showToast("提交成功");
        sendMessage(SendMessageEvent(MessageConfig.FINISH_MEDICAL_SIGN));
        finish();
      }else{
        showToast(map["msg"].toString());
      }
    } catch (e) {
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }
}